package com.ruoyi.web.controller.dm;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.exception.DataRepeatException;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.dm.domain.NormalFileVO;
import com.ruoyi.dm.domain.RadioStationParam;
import com.ruoyi.dm.service.INormalFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;

/**
 * 常数文件
 */
@Slf4j
@RestController
@RequestMapping("/collect/noraml")
@Api(tags = "数据采集-常数文件")
public class NoramlFileController extends BaseController {

    @Autowired
    private INormalFileService normalFileService;

    /**
     * 上传
     *
     * @param files
     * @param fileType
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("数据导入")
    @ApiImplicitParam(value = "上传文件",name = "files",dataTypeClass = File.class,required = true)
    public R<Boolean> collect(@RequestPart @RequestParam MultipartFile[] files,
                               @ApiParam(value = "文件类型", name = "fileType") @RequestParam(value = "fileType", required = false) String fileType) {
        try {
            List<String> res = normalFileService.input(files, fileType);
            if (res.size()>0) {
                return R.fail(CollectionUtil.join(res,",")+"重复上传,不再处理");
            }
            return R.ok(true, "开始解析");
        } catch (DataRepeatException e) {
            return R.fail("请勿重复上传");
        }
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
    @ApiOperation("数据删除(单个)")
    public R<Boolean> del(@ApiParam(value = "id", name = "id") @RequestParam(value = "id", required = true) Long id) {
        return R.ok(normalFileService.delFile(id));
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @GetMapping("/delBatch")
    @ApiOperation("数据删除(批量)")
    public R<Boolean> delBath(@ApiParam(value = "id集合", name = "ids") @RequestParam List<Long> ids) {
        return R.ok(normalFileService.delBatch(ids));
    }

    /**
     * 检索
     *
     * @param param
     * @return
     */
    @ApiOperation("数据查询")
    @PostMapping("/query")
    public R<IPage<NormalFileVO>> query(@Validated @RequestBody RadioStationParam param) {
        IPage<NormalFileVO> query = normalFileService.query(param);
        return R.ok(query);
    }

    /**
     * 查看评估结果
     *
     * @param id
     * @return
     */
    @GetMapping("/preview")
    @ApiOperation("数据预览")
    public void preview(@ApiParam(value = "id", name = "id") @RequestParam(value = "id", required = true) Long id, HttpServletResponse response) {
        String s = normalFileService.preview(id);
        try {
            response.setContentType(MediaType.TEXT_PLAIN_VALUE);
            FileUtils.writeBytes(s, response.getOutputStream());
        } catch (Exception e) {
            log.error("下载失败", e);
        }
    }


    /**
     * 下载,返回文件路径
     *
     * @param id
     * @return
     */
    @ApiOperation("数据下载(单个)")
    @GetMapping("/download")
    public void download(@ApiParam(value = "id", name = "id") @RequestParam(value = "id", required = true) Long id, HttpServletResponse response) {
        String s = normalFileService.download(id);
        File file = new File(s);
        try {
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            FileUtils.setAttachmentResponseHeader(response, file.getName());
            FileUtils.writeBytes(s, response.getOutputStream());
        } catch (Exception e) {
            log.error("下载失败", e);
        }
    }

    /**
     * 批量下载
     *
     * @param ids
     * @return 文件压缩包路径
     */
    @ApiOperation("数据下载(批量)")
    @PostMapping("/downloadBatch")
    public void downloadBatch(@ApiParam(value = "文件类型", name = "ids") @RequestParam List<Long> ids, HttpServletResponse response) {

        String s = normalFileService.downloadBatch(ids);
        File file = new File(s);
        try {
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            FileUtils.setAttachmentResponseHeader(response, file.getName());
            FileUtils.writeBytes(s, response.getOutputStream());
        } catch (Exception e) {
            log.error("下载失败", e);
        }
    }
}
